package com.ifengchao.service.impl;

import com.ifengchao.dao.iface.DealReptileDao;
import com.ifengchao.pojo.HConfig;
import com.ifengchao.pojo.HCrawlLog;
import com.ifengchao.service.DealIfanrService;
import com.ifengchao.util.ReptileUtil;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * @Author YiMing
 * @Description:
 * @Date: Created in 上午9:36 2017/9/25
 * @Modified By
 */
@Service
public class DealIfanrServiceImpl implements DealIfanrService {
	private static final Logger logger = Logger.getLogger(DealIfanrServiceImpl.class);

	@Autowired(required = true)
	private DealReptileDao dealReptileDao;

	private static String hyzUrl = null; //慧驿站url

	@Override
	public void getWeChatData() {

		//查询爬虫配置开关是否开启
		Map<String, String> map = dealReptileDao.querySetting();

		String flag = map.get("reptile_thread_flag");

		hyzUrl = map.get("hyzUrl");

		logger.info("开关状态是:" + flag);

		//用于存储爬虫运行时间list
		ArrayList<HCrawlLog> hCrawlLogs = new ArrayList<>();

		if ("Y".equals(flag)) {
			List<HConfig> hConfigList = dealReptileDao.queryConfig();
			for (HConfig hConfig : hConfigList) {
				if ("P00013".equals(hConfig.getPlatNo())) {
					//微信 爱范儿
					ReptileUtil.getMessage(hCrawlLogs,hConfig,"爱范儿",dealReptileDao);
				}
			}

			if (hCrawlLogs.size() > 0) {
				logger.info("开始插入爬虫记录表");
				dealReptileDao.insertCrawlLog(hCrawlLogs);
			}
		} else {
			logger.info("=========爬虫配置开关没有开启========");
		}

	}

	/**
	 * Gets wechat data.
	 *
	 * @param dealReptileDao the deal reptile dao
	 * @param hConfig        the h config
	 * @throws Exception the exception
	 */
	public static void getWeChatData(DealReptileDao dealReptileDao, HConfig hConfig) throws Exception {

		//根据网址获取首页html信息,并排除不符合日期和关键字的文章
		ArrayList<Map<String, String>> indexHtml = ReptileUtil.getIndexHtml(dealReptileDao,hConfig);

		//微信获取文章内容并执行插入
		ReptileUtil.getWeChatMessage(hConfig,dealReptileDao,indexHtml,hyzUrl);

	}
}
